package com.itheima.service.pmsService.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.commons.PageBean;
import com.itheima.domain.db.pms.PmsBrand;
import com.itheima.mapper.pmsMapper.BrandMapper;
import com.itheima.service.pmsService.BrandService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class BrandServiceImpl implements BrandService {

    @Autowired
    BrandMapper brandMapper;

    //根据品牌名称分页获取品牌列表
    @Override
    public PageBean<PmsBrand> getBrandList(String keyword, Integer pageNum, Integer pageSize) {

        Page<PmsBrand> page = new Page<>(pageNum, pageSize);

        QueryWrapper<PmsBrand> qw = new QueryWrapper();
        qw.eq(StrUtil.isNotEmpty(keyword), "name", keyword);
        qw.orderByAsc("sort");
        brandMapper.selectPage(page, qw);
        PageBean<PmsBrand> pmsBrandPageBean = new PageBean<>(page);

        return pmsBrandPageBean;
    }

    @Override
    public void saveBrand(PmsBrand brand) {
        brandMapper.insert(brand);
    }

    @Override
    public void deleteBrand(List<Long> ids) {
        brandMapper.deleteBatchIds(ids);
    }

    @Override
    public void deleteById(Integer id) {
        brandMapper.deleteById(id);
    }

    @Override
    public List<PmsBrand> listAll() {
        QueryWrapper<PmsBrand> qw = new QueryWrapper<>();
        List<PmsBrand> pmsBrand = brandMapper.selectList(qw);
        return pmsBrand;
    }

    @Override
    public void updateFactoryStatus(Integer factoryStatus, Integer[] ids) {
        for (Integer id : ids) {
            UpdateWrapper<PmsBrand> qw = new UpdateWrapper();
            qw.eq("id", id);
            qw.set("factory_status", factoryStatus);
            brandMapper.update(null, qw);
        }
    }

    @Override
    public void updateShowStatus(Integer showStatus, Integer[] ids) {
        for (Integer id : ids) {
            LambdaUpdateWrapper<PmsBrand> lqw = new LambdaUpdateWrapper();
            lqw.eq(ObjectUtil.isNotEmpty(id), PmsBrand::getId, id);
            lqw.set(PmsBrand::getShowStatus, showStatus);
            brandMapper.update(null, lqw);
        }
    }

    @Override
    public void updateById(PmsBrand brand) {
        brandMapper.updateById(brand);
    }

    @Override
    public PmsBrand getById(Long id) {
        LambdaQueryWrapper<PmsBrand> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PmsBrand::getId, id);
        return brandMapper.selectOne(lqw);
    }
}
